package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import easier.log.logger.Logger;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.subjects.PublishSubject;

public class MainActivity31 extends AppCompatActivity {

    private final PublishSubject< Object > mPublishSubject = PublishSubject.create();
    private Button mClick;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity31.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main31 );

        mPublishSubject
            .doOnError( new Consumer< Throwable >() {
                @Override
                public void accept( Throwable throwable ) throws Throwable {

                    Logger.d( "error:" + throwable.getMessage() );
                }
            } )
            .doOnComplete( new Action() {
                @Override
                public void run() throws Throwable {

                    Logger.d( "complete:" );
                }
            } );

        initView();
    }

    private void initView() {

        mClick = (Button) findViewById( R.id.click );
        mClick.setOnClickListener( new OnClickListener() {


            @Override
            public void onClick( View v ) {

                mPublishSubject
                    .flatMap( new Function< Object, ObservableSource< ? > >() {

                        @Override
                        public ObservableSource< ? > apply( Object o ) throws Throwable {

                            return Observable.fromArray( o );
                        }
                    } )
                    .subscribe( new Observer< Object >() {

                        @Override
                        public void onError( Throwable e ) {

                            Logger.d( e.getMessage() + ":subscribe" );
                        }

                        @Override
                        public void onComplete() {

                            Logger.d( "complete" );
                        }

                        @Override
                        public void onSubscribe( @NonNull Disposable d ) {

                        }

                        @Override
                        public void onNext( Object o ) {

                            Logger.d( o + ":subscribe" );
                        }
                    } );

                mPublishSubject.onNext( "Hello" );
                mPublishSubject.onComplete();
            }
        } );
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
}